package com.atguigu.c_comparable;

import java.util.Objects;

public class Person implements Comparable<Person>{
    private String name;
    private int age;
    private int chinese;
    private int math;
    private int english;

    public Person() {
    }

    public Person(String name, int age, int chinese, int math, int english) {
        this.name = name;
        this.age = age;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getChinese() {
        return chinese;
    }

    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public int getEnglish() {
        return english;
    }

    public void setEnglish(int english) {
        this.english = english;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && chinese == person.chinese && math == person.math && english == person.english && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, chinese, math, english);
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", chinese=" + chinese +
                ", math=" + math +
                ", english=" + english +
                '}' + " 总分 : " + getSum();
    }

    //定义方法 求总分
    public int getSum(){
        return chinese + math + english;
    }

    @Override
    public int compareTo(Person o) {
        /*
            新 : this
            老 : o
        *   新 - 老 : 升序
        *
        *   老 - 新 : 降序
        * */
        //多条件排序
        //1. 总分降序
        int result = o.getSum() - this.getSum();
        //2. 语文降序
        result = result == 0 ? o.chinese - this.chinese : result;
        //3. 数学降序
        result = result == 0 ? o.math - this.math : result;
        //4. 年龄升序
        result = result == 0 ? this.age - o.age : result;
        //5. 名字的首字母 [不要用中文,用英文名]
        result = result == 0 ? this.name.compareToIgnoreCase(o.name) : result;
        return result;
    }
}
